package com.snake.springboot.authority.service.impl;

import com.snake.springboot.authority.api.model.AuthRoleMenuModel;
import com.snake.springboot.authority.api.request.AuthRoleMenuSave;
import com.snake.springboot.authority.api.service.AuthRoleMenuService;
import com.snake.springboot.authority.dao.mybatis.entity.AuthRoleMenuDO;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthRoleMenuMapper;
import com.snake.springboot.authority.service.assembler.AuthRoleMenuAssembler;
import org.snake.common.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 类 {@code AuthRoleMenuServiceImpl}  <br> .
 * <p>角色与菜单关系服务实现类
 * 创建时间：2024/5/27 16:22
 *
 * @author Yzhui
 * @see
 */
@Service
public class AuthRoleMenuServiceImpl implements AuthRoleMenuService {

    private AuthRoleMenuMapper authRoleMenuMapper;

    @Autowired
    public AuthRoleMenuServiceImpl(AuthRoleMenuMapper authRoleMenuMapper) {
        this.authRoleMenuMapper = authRoleMenuMapper;
    }

    @Override
    public List<AuthRoleMenuModel> queryRoleMenuRef(String roleId) {
        List<AuthRoleMenuDO> records = authRoleMenuMapper.findByCondition(new AuthRoleMenuDO(roleId, null));
        return AuthRoleMenuAssembler.assembleAuthRoleMenuModelList(records);
    }

    @Override
    public List<AuthRoleMenuModel> queryRoleMenuRef(AuthRoleMenuModel model) {
        AuthRoleMenuDO condition = AuthRoleMenuAssembler.assembleAuthRoleMenuDO(model);
        List<AuthRoleMenuDO> records = authRoleMenuMapper.findByMenuCondition(condition);
        List<AuthRoleMenuModel> result = AuthRoleMenuAssembler.assembleAuthRoleMenuModelList(records);
        return result;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int saveRoleMenuRef(AuthRoleMenuSave params) {
        int result = 0;
        List<AuthRoleMenuDO> records = AuthRoleMenuAssembler.createAuthRoleMenuDOList(params);
        authRoleMenuMapper.deleteByRoleAndSys(params.getRoleId(), params.getSystemId(), null);
        if(!CollectionUtils.isNullOrEmpty(records)){
            result = authRoleMenuMapper.insertBatch(records);
        }
        return result;
    }
}
